package BuncoPlus;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

import javax.swing.*;

import Framework.*;

@SuppressWarnings("serial")
public class NewGameFrame extends JFrame 
{
	ActionListener btnOkListener;
	ActionListener btnCancelListener;
	ActionListener cboListener; 
	ArrayList<JTextField> txtFields;
	JComboBox cboNombreJoueurs;
	Game game;
	
	public NewGameFrame(Framework.Game g)
	{
		game = g;
		
		/******************************************************
		 ********************* INTERFACE **********************
		 ******************************************************/	
		this.setSize(400, 250);
		this.setLayout(null);
		this.setDefaultCloseOperation(1);
		this.setResizable(false);
		
		JPanel pan = new JPanel();
		pan.setSize(400, 250);
		pan.setLocation(0, 0);
		pan.setLayout(null);
		
		this.add(pan);

		/******************************************************
		 ********************** EVENTS ************************
		 ******************************************************/
		btnOkListener = new ActionListener() 
		{			
			@Override
			public void actionPerformed(ActionEvent e) 
			{
				for (JTextField txtField : txtFields)
				{
					if (txtField.isEnabled())
						game.AddPlayer(txtField.getText());
				}
				closeWindow();
			}
		};
		btnCancelListener = new ActionListener() 
		{			
			@Override
			public void actionPerformed(ActionEvent e) 
			{
				closeWindow();
			}
		};
		cboListener = new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				int i = 0;
				
				if (txtFields != null)
				{
					for (JTextField txtField : txtFields)
					{
						if ((i - 1) <= cboNombreJoueurs.getSelectedIndex())
							txtField.setEnabled(true);
						else
							txtField.setEnabled(false);
						
						++i;
					}
				}
			}
		};
		

		/******************************************************
		 ***************** NOMBRE DE JOUEURS ******************
		 ******************************************************/
		JLabel lbl1 = new JLabel("Nombre de joueurs");
		lbl1.setLocation(20, 20);
		lbl1.setSize(150, 15);
		pan.add(lbl1);
		
		cboNombreJoueurs = new JComboBox();
		cboNombreJoueurs.setLocation(140, 18);
		cboNombreJoueurs.setSize(150, 20);
		cboNombreJoueurs.addActionListener(cboListener);
		for (int i = 1; i < g.getNumberMaxOfPlayers(); ++i)
			cboNombreJoueurs.addItem("" + (i + 1) + " joueur");
		cboNombreJoueurs.setSelectedIndex(g.getNumberMaxOfPlayers() - 2);
		
		pan.add(cboNombreJoueurs);
		

		/******************************************************
		 ********************** JOUEURS ***********************
		 ******************************************************/
		JLabel lblJ1 = new JLabel("Joueur 1");
		lblJ1.setLocation(20, 70);
		lblJ1.setSize(150, 15);
		pan.add(lblJ1);
		
		JTextField txtJ1 = new JTextField("Nom1");
		txtJ1.setLocation(20, 88);
		txtJ1.setSize(150, 20);
		pan.add(txtJ1);
		
		JLabel lblJ3 = new JLabel("Joueur 3");
		lblJ3.setLocation(20, 118);
		lblJ3.setSize(150, 15);
		pan.add(lblJ3);
		
		JTextField txtJ3 = new JTextField("Nom3");
		txtJ3.setLocation(20, 136);
		txtJ3.setSize(150, 20);
		pan.add(txtJ3);

		JLabel lblJ2 = new JLabel("Joueur 2");
		lblJ2.setLocation(220, 70);
		lblJ2.setSize(150, 15);
		pan.add(lblJ2);
		
		JTextField txtJ2 = new JTextField("Nom2");
		txtJ2.setLocation(220, 88);
		txtJ2.setSize(150, 20);
		pan.add(txtJ2);
		
		JLabel lblJ4 = new JLabel("Joueur 4");
		lblJ4.setLocation(220, 118);
		lblJ4.setSize(150, 15);
		pan.add(lblJ4);
		
		JTextField txtJ4 = new JTextField("Nom4");
		txtJ4.setLocation(220, 136);
		txtJ4.setSize(150, 20);
		pan.add(txtJ4);

		txtFields = new ArrayList<JTextField>();
		txtFields.add(txtJ1);
		txtFields.add(txtJ2);
		txtFields.add(txtJ3);
		txtFields.add(txtJ4);
		
		/******************************************************
		 ********************* BUTTONS ************************
		 ******************************************************/		
		JButton btnOK = new JButton("OK");
		btnOK.setSize(new Dimension(100, 33));
		btnOK.setLocation(175, 180);
		btnOK.addActionListener(btnOkListener);
		pan.add(btnOK);		

		JButton btnCancel = new JButton("Annuler");
		btnCancel.setSize(new Dimension(100, 33));
		btnCancel.setLocation(285, 180);
		btnCancel.addActionListener(btnCancelListener);
		pan.add(btnCancel);		
	}
	
	private void closeWindow()
	{
		this.dispose();
	}
}
